<?php 
namespace Gigasweb\Wordpress\Metabox;

class PostChecklist {
	private $_PostType;
	private $_Title;
	private $_Tip;
	private $_Name;
	private $_ParentPost;
	
	function __construct( $params ) {
		$defaults = array(
				'title' => '',
				'name' => '',
				'tip' => '',
				'post_type' => 'post',
				'parent_post' => 0,
		);
		$params = array_merge( $defaults, $params );
		
		$this->_PostType = $params['post_type'];
		$this->_Title = $params['title'];
		$this->_Tip = $params['tip'];
		$this->_Name = $params['name'];
		$this->_ParentPost = $params['parent_post'];
	}
	
	public function Render( $checked = array() ) {
		$posts = get_posts( array( 
				'post_parent' => $this->_ParentPost, 
				'post_type' => $this->_PostType, 
				'numberposts' => -1,
				'posts_per_apge' => -1, 
				'post_status' => 'any'
			) );
		?>
	    <div class="inside">
	        <p><strong><?php echo $this->_Title; ?></strong></p>
	        <p class="help"><?php echo $this->_Tip; ?></p>
	        <div class="taxonomydiv">
	        	<div class="tabs-panel">
	        		<ul class="categorychecklist form-no-clear">
				        <?php 
				        if( $posts ) {
							foreach( $posts as $p ) {
								?><li id="item-<?php echo $p->post_type; ?>-<?php echo $p->ID; ?>">
									<label class="selectit">
										<input value="<?php echo $p->ID; ?>" type="checkbox" name="<?php echo $this->_Name; ?>[]" <?php echo $checked ? ( in_array( $p->ID, $checked ) ? 'checked="checked"' : '' ) : '';  ?> />
										<?php echo apply_filters( 'the_title', $p->post_title ); ?>
									</label>
								  </li><?php 
							}
						}
						?>
					</ul>
				</div>
			</div>
		</div>
		<?php
	}
}